//
//  RootViewController.m
//  sample
//
//  Created by crpatte2 on 11/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "InputWordsViewController.h"
#import "CreateOwnViewController.h"
//#import <UIAlertView.h>

@implementation RootViewController

@synthesize use, inputtedTextExternal, typeCount, inputtedTextInternal;

- (void)viewDidLoad
{
    [super viewDidLoad];
    //excerpts = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"excerpts"
     //                                                                           ofType:@"plist"]];
    //[excerpts retain];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [documentPaths objectAtIndex:0];
    NSString *filename = [NSString stringWithFormat:@"%@/excerpts.plist", docDirectory];
    excerpts = [NSArray arrayWithContentsOfFile:filename];
    [excerpts retain];
    [self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [excerpts count] + 1;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *imageName = @"table_cell";
    int imageSelect;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    int row = indexPath.row;
    imageSelect = row%4 + 2;
    imageName = [imageName stringByAppendingFormat:@"%d.jpg", imageSelect];
    
    [cell.textLabel setFont:[UIFont fontWithName:@"MarkerFelt-Thin"
                                  size:20.0]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    cell.textLabel.backgroundColor = [UIColor clearColor];

    if (row == 0) {
        cell.textLabel.text = @"Create your own!";
    }
    else {
        NSDictionary *excerpt = [excerpts objectAtIndex:(row - 1)];
    
        cell.textLabel.text = [excerpt objectForKey:@"title"];
    }
      
    // Configure the cell.
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    int row = indexPath.row;
        if (row == 0) {
            CreateOwnViewController *createVC = [[CreateOwnViewController alloc] initWithNibName:@"CreateOwnViewController" bundle:nil];
            createVC.inputtedTextExternal = @"";
            createVC.inputtedTextInternal = @"";
            [self.navigationController pushViewController:createVC animated:YES];
            [createVC release];
        }
        else{
            InputWordsViewController *inputVC = [[InputWordsViewController alloc] initWithNibName:@"InputWordsViewController"    bundle:nil];
            inputVC.excerpt = [excerpts objectAtIndex:(row - 1)];
            [self.navigationController pushViewController:inputVC animated:YES];
            [inputVC release];
        }
    
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}

@end
